package com.yugao.fintech.antelope.infra;

import com.yugao.fintech.antelope.infra.common.ScanScene;
import com.yugao.fintech.antelope.infra.file.service.FileRecordService;
import com.yugao.fintech.antelope.infra.file.service.MediaMaterialService;
import com.yugao.fintech.antelope.infra.group.service.DataGroupService;
import com.yugao.fintech.antelope.infra.idgenerator.AutoRegisterWorkerId;
import com.yugao.fintech.antelope.infra.module.file.FileProperties;
import com.yugao.fintech.antelope.infra.module.file.constants.FileCons;
import com.yugao.fintech.antelope.infra.module.file.model.rqrs.FileRecordResp;
import com.yugao.fintech.antelope.infra.module.file.model.rqrs.MediaMaterialResp;
import com.yugao.fintech.antelope.infra.weixin.mp.handler.WxMpHandler;
import com.yugao.fintech.antelope.base.model.enums.DataGroupBizTypeEnum;
import com.yugao.fintech.framework.assistant.utils.UrlUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
@Primary
@Service
public class InfraApiImpl implements InfraApi {
    @Autowired
    private DataGroupService groupService;
    @Autowired
    private FileProperties fileProperties;
    @Autowired
    private FileRecordService fileRecordService;
    @Autowired
    @Lazy
    private WxMpHandler wxMpHandler;
    @Autowired
    private MediaMaterialService materialService;
    @Autowired
    private AutoRegisterWorkerId autoRegisterWorkerId;

    @Override
    public MediaMaterialResp getMediaMaterial(Long id) {
        return materialService.getMaterial(id);
    }

    @Override
    public Long saveDataGroup(DataGroupSaveReq req) {
        return groupService.saveGroup(req);
    }

    @Override
    public List<DataGroupResp> listChildrenDataGroup(Long parentId) {
        return groupService.listChildrenGroup(parentId);
    }

    @Override
    public List<DataGroupResp> listDataGroup(DataGroupListReq req) {
        return groupService.listGroup(req);
    }

    @Override
    public boolean updateDataGroup(DataGroupUpdateReq req) {
        return groupService.updateGroup(req);
    }

    @Override
    public DataGroupResp getDataGroup(Long id) {
        return groupService.getGroup(id);
    }

    @Override
    public boolean deleteDataGroup(DataGroupBizTypeEnum type, Long id) {
        DataGroupDeleteReq req = new DataGroupDeleteReq();
        req.setBizType(type.getType());
        req.setIds(Collections.singletonList(id));
        return groupService.deleteGroup(req);
    }

    @Override
    public boolean batchDeleteDataGroup(DataGroupBizTypeEnum type, List<Long> ids) {
        DataGroupDeleteReq req = new DataGroupDeleteReq();
        req.setBizType(type.getType());
        req.setIds(ids);
        return groupService.deleteGroup(req);
    }

    @Override
    public String getFileIntranetUrl(String filePath) {
        filePath = filePath.replace(FileCons.UPLOAD_BASE_URL, "");
        return UrlUtils.removeRepeatSlashOfUrl(fileProperties.getIntranetUrl() + "/" + filePath);
    }

    @Override
    public FileRecordResp getFileRecordByPath(String path, boolean isIntranet) {
        return fileRecordService.getRecordByPath(path, isIntranet);
    }

    @Override
    public void deleteFileRecordByPath(String path) {
        fileRecordService.deleteRecordByPath(path);
    }

    @Override
    public String getMpQrcode(ScanScene req) {
        return wxMpHandler.getQrcode(req);
    }

    @Override
    public Short getIdGenNextWorkId() {
        return autoRegisterWorkerId.getNextWorkId();
    }

    @Override
    public void refreshIdGenWorkId(Short workId) {
        autoRegisterWorkerId.refresh(String.valueOf(workId));
    }
}
